/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.foridev.gec.gui;

import com.foridev.gec.model.User;
import com.foridev.gec.security.Authenticator;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.fxml.JavaFXBuilderFactory;
import javafx.geometry.Rectangle2D;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.BorderPane;
import javafx.stage.Screen;
import javafx.stage.Stage;

/**
 *
 * @author amine
 */
public class Main extends Application {
    private Stage stage;
    private User loggedUser;
     private static Main instance;

    public Main() {
        instance = this;
    }
    
    public static Main getInstance() {
        return instance;
    }
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }
    public User getLoggedUser() {
        return loggedUser;
    }
    public boolean userLogging(String userId, String password){
        if (Authenticator.validate(userId, password)) {
            loggedUser = User.of(userId);
            gotoMasterPage();
            return true;
        } 
        return false;
    }
     private void gotoLogin() {
        try {
            replaceSceneContent("login.fxml");
        } catch (Exception ex) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void gotoMasterPage() {
        try {
            replaceSceneContent("MasterPage.fxml");
        } catch (Exception ex) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, null, ex);
        }
    }
    public void gotoNaissanceRecord() {
        try {
            replaceMasterPageCenterContent("NaissanceRecord.fxml");
        } catch (Exception ex) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, null, ex);
        }
    }
     public void gotoFindPeople() {
        try {
            replaceMasterPageCenterContent("findPeople.fxml");
        } catch (Exception ex) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, null, ex);
        }
    }
     
    @Override
    public void start(Stage primaryStage) throws Exception{
        primaryStage.setTitle("GEC!!!!!!!!!!!!!!!!!!!!!");
        stage = primaryStage;
        gotoLogin();
        primaryStage.show();

    }
    
    private Parent replaceSceneContent(String fxml) throws Exception {
        Parent page = (Parent) FXMLLoader.load(getClass().getResource(fxml), null, new JavaFXBuilderFactory());
        Scene scene = stage.getScene();
        if (scene == null) {
            scene = new Scene(page, 700, 550);
            scene.getStylesheets().add(getClass().getResource("style.css").toExternalForm());
            stage.setScene(scene);
        } else {
            stage.getScene().setRoot(page);
            //stage.setFullScreen(true);

            Screen screen = Screen.getPrimary();
            Rectangle2D bounds = screen.getVisualBounds();

            stage.setX(bounds.getMinX());
            stage.setY(bounds.getMinY());
            stage.setWidth(bounds.getWidth());
            stage.setHeight(bounds.getHeight());
            
        }
       //stage.sizeToScene();
        return page;
    }
        private Parent replaceMasterPageCenterContent(String fxml) throws Exception {
            Parent page = (Parent) FXMLLoader.load(getClass().getResource(fxml), null, new JavaFXBuilderFactory()); 
            AnchorPane rootAnchorPane=((AnchorPane)stage.getScene().getRoot());
            BorderPane borderPaint=(BorderPane)rootAnchorPane.getChildren().get(0);
            borderPaint.setCenter(page);
        
        return page;
    }
}
